﻿#region License

// Droid Manager - An Android Phone Tools Suite
// Copyright (C) 2011
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System.Runtime.InteropServices;
using System.Text;

namespace DroidManager.Data.Utils
{
    /// <summary>
    /// Manages Ini Files
    /// </summary>
    public class IniFile
    {
        private string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
                                                             string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                                                          string key, string def, StringBuilder retVal,
                                                          int size, string filePath);

        internal IniFile(string INIPath)
        {
            this.path = INIPath;
        }

        /// <summary>
        /// Writes a Value into ini file
        /// <example>
        /// <code>
        /// [Section]
        /// Key = Value
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="Section">Section Name</param>
        /// <param name="Key">Key Name</param>
        /// <param name="Value">Value Content</param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>
        /// Reads a Value into ini file
        /// <example>
        /// <code>
        /// [Section]
        /// Key = Value
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="Section">Section Name</param>
        /// <param name="Key">Key Name</param>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp,
                                            255, this.path);
            return temp.ToString();
        }
    }
}